package summary;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: 海琳琦
 * @Date: 2022/3/16 9:04
 * https://leetcode-cn.com/problems/subsets/
 */
public class Title78 {

    List<List<Integer>> result = new ArrayList<>();
    LinkedList<Integer> list = new LinkedList<>();

    public List<List<Integer>> subsets(int[] nums) {
        backTracking(nums, 0);
        result.add(new ArrayList<>());
        return result;
    }

    private void backTracking(int[] nums, int start) {
        result.add(new ArrayList<>(list));
        for (int i = start; i < nums.length; i++) {
            list.add(nums[i]);
            backTracking(nums, i + 1);
            list.removeLast();
        }
    }


    public List<List<Integer>> subsets2(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        LinkedList<Integer> temp = new LinkedList<>();
        backTracking1(result, temp, nums, 0);
        return result;
    }

    private void backTracking1(List<List<Integer>> result, LinkedList<Integer> temp, int[] nums, int start) {
        result.add(new ArrayList<>(temp));
        for (int i = start; i < nums.length; i++) {
            temp.add(nums[i]);
            backTracking1(result, temp, nums, i + 1);
            temp.removeLast();
        }
    }

    public static void main(String[] args) {

    }
}
